package com.muyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.constant.CacheConstants;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.redis.service.RedisService;
import com.muyu.system.domain.SysConfig;
import com.muyu.system.domain.model.SysConfigPageQueryModel;
import com.muyu.system.mapper.SysConfigMapper;
import com.muyu.system.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 系统配置业务实现层
 * @author DongZl
 * @description: 配置plus业务实现层
 * @Date 2023-11-13 上午 10:06
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig>
        implements SysConfigService {

    @Autowired
    private RedisService redisService;

    @Override
    public PageQueryModel<SysConfig> pageQuery (SysConfigPageQueryModel pageQueryModel) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(pageQueryModel.getConfigName()),SysConfig::getConfigName, pageQueryModel.getConfigName());
        queryWrapper.like(StringUtils.isNotEmpty(pageQueryModel.getConfigType()),SysConfig::getConfigType, pageQueryModel.getConfigType());
        queryWrapper.like(StringUtils.isNotEmpty(pageQueryModel.getConfigKey()),SysConfig::getConfigKey, pageQueryModel.getConfigKey());
        queryWrapper.gt(Objects.nonNull(pageQueryModel.getBeginTime()),SysConfig::getCreateTime, pageQueryModel.getBeginTime());
        queryWrapper.lt(Objects.nonNull(pageQueryModel.getEndTime()),SysConfig::getCreateTime, pageQueryModel.getEndTime());
        Page<SysConfig> page = this.page(pageQueryModel.buildPage(), queryWrapper);
        return PageQueryModel.of(page);
    }

    /**
     * 通过Key进行查询值
     *
     * @param configKey
     *
     * @return
     */
    @Override
    public String selectConfigByKey (String configKey) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(configKey, "key不可为空");
        queryWrapper.eq(SysConfig::getConfigKey, configKey);
        SysConfig sysConfig = this.getOne(queryWrapper);
        return sysConfig.getConfigValue();
    }

    @Override
    public boolean checkConfigKeyUnique (String configKey) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getConfigKey, configKey);
        return this.count(queryWrapper) == 0;
    }

    @Override
    public void resetConfigCache () {
        this.clearConfigCache();
        this.loadingConfigCache();
    }

    /**
     * 清空参数缓存数据
     */
    @Override
    public void clearConfigCache () {
        Collection<String> keys = redisService.keys(CacheConstants.SYS_CONFIG_KEY + "*");
        redisService.deleteObject(keys);
    }

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache () {
        List<SysConfig> configsList = this.list();
        for (SysConfig config : configsList) {
            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     *
     * @return 缓存键key
     */
    private String getCacheKey (String configKey) {
        return CacheConstants.SYS_CONFIG_KEY + configKey;
    }
}
